package leetcode_900;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *@author 周杨
 *MostCommonWord_819 给定一个段落 返回除了ban集合里的 出现单词次数最多的词
 *describe:AC 56% 很常规
 *2018年10月29日 上午9:53:35
 */
public class MostCommonWord_819 {
	public static void main(String[] args) {
		MostCommonWord_819 test=new MostCommonWord_819();
		System.out.println(test.mostCommonWord("Bob hit a ball, the hit BALL flew far after it was hit.",
				new String[] {"hit"}));
	}
	int diff='a'-'A';
	/**
	 * describe:AC 56%
	 * 2018年10月29日 上午9:51:12
	 */
	public String mostCommonWord(String paragraph, String[] banned) {
        List<String> list=Arrays.asList(banned);
        int max=0;
        String maxString="";
        Map<String,Integer> map=new HashMap<String,Integer>();
        String[] words=paragraph.split("\\W+");//用正则过滤
        for(String word:words) {
        	word=help(word);
        	if(list.contains(word))
        		continue;
        	else {
        		int count=map.getOrDefault(word, 0)+1;
        		if(max<count) {
        			max=count;
        			maxString=word;
        		}
        		map.put(word, count);
        	}
        }
        return maxString;
    }
	
	private String help(String s) {
		char []chars=s.toCharArray();
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<chars.length;++i) {
			if(chars[i]<='Z'&&chars[i]>='A')
				sb.append((char)(chars[i]+diff));
			else if(chars[i]<='z'&&chars[i]>='a')
				sb.append(chars[i]);
		}
		return sb.toString();
	}
}
